<script setup lang="ts">

import {message} from "ant-design-vue";
import {getCurrentInstance} from "vue";
import BaseApp from "~/contentScripts/views/sycm/baseApp.vue";
import SearchRankModal from "~/contentScripts/views/sycm/market_monitor/search_rank/SearchRankModal.vue";
import User from "~/contentScripts/views/tao/list/components/user.vue";
import {isLogin} from "~/logic";
import SYCM from "~/utils/ali/sycm";

const loginVisible = ref<boolean>(false);
const visible = ref<boolean>(false);
const data = ref<any>([]);
const tab1 = ref<any>(null);
const tab2 = ref<any>(null);
const loading = ref(false);
const __option = getCurrentInstance()?.appContext.provides.option;
const handle = async () => {
  if (isLogin({
    emit: () => {
      loginVisible.value = true;
    },
  }, __option)) {
    loading.value = true;
    let cate = document.querySelector('.ebase-FaCommonFilter-module__left .oui-pro-common-picker .common-picker-header')?.title;
    let tab = await SYCM.getInternalFiberData('.ebase-FaCommonFilter-module__left>div>span.oui-tab-switch', 'memoizedProps.children[0]._owner.memoizedProps');
    if (!tab) {
      message.error('获取父TAB失败,请重试');
      loading.value = false;
      return;
    }
    tab1.value = tab;
    let tabChild = await SYCM.getInternalFiberData('.op-mc-search-rank-content-tab>span.oui-tab-switch', 'memoizedProps.children[0]._owner.memoizedProps');
    if (!tabChild) {
      message.error('获取子TAB失败,请重试');
      loading.value = false;
      return;
    }
    tab2.value = tabChild;
    // console.log(cate, tab, tabChild);
    let tableData = await SYCM.getInternalFiberData('#sycm-mc-mq-item-rank > div.oui-card-content', 'memoizedProps.children._owner.memoizedProps.tableData');
    if (!tableData) {
      message.error('获取数据失败,请重试');
      loading.value = false;
      return;
    }
    // // console.log(tableData);
    // if (tableData.data.length == 0) {
    //   message.error('无需要转化数据！');
    //   loading.value = false;
    //   return;
    // }
    SYCM.assignData(tableData.data);
    tableData.data.map((item: any) => {
      item.date = tableData.params.dateRange.split('|');
      if (item.date[0] == item.date[1]) {
        item.date = item.date[0];
      } else {
        item.date = item.date[0] + '~' + item.date[1];
      }
      item.cate = cate;
      if (typeof item.clickHits !== 'undefined') {
        item.clickRate = parseFloat(parseFloat(String(item.clickRate * 100)).toFixed(2));
      }
      if (typeof item.payRate !== 'undefined') {
        item.payRate = parseFloat(parseFloat(String(item.payRate * 100)).toFixed(2));
      }
      if (typeof item.seRiseRate !== 'undefined') {
        item.seRiseRate = parseFloat(parseFloat(String(item.seRiseRate * 100)).toFixed(2));
      }
      if (typeof item.avgWordClickRate!=='undefined'){
        item.avgWordClickRate = parseFloat(parseFloat(String(item.avgWordClickRate * 100)).toFixed(2));
      }
      if (typeof item.avgWordPayRate!=='undefined'){
        item.avgWordPayRate = parseFloat(parseFloat(String(item.avgWordPayRate * 100)).toFixed(2));
      }
      if (typeof item.avgWordClickHits_tran!='undefined' && item.avgWordPayRate!=='undefined'){
        item.avgWordPayUserNumber = Math.round(item.avgWordClickHits_tran * (item.avgWordPayRate / 100));
        if (isNaN(item.avgWordPayUserNumber)) {
          item.avgWordPayUserNumber = 0;
        }
      }
      if (typeof item.clickHits !== 'undefined' && typeof item.payRate !== 'undefined') {
        // clickHits * (payRate / 100) = payUserNumber 付款人数 = 点击人数 * 转化率 / 100  四舍五入
        item.payUserNumber = Math.round(item.clickHits_tran * (item.payRate / 100));
        if (isNaN(item.payUserNumber)) {
          item.payUserNumber = 0;
        }
      }
      if (isNaN(item.payUserNumber)){
        item.payUserNumber = 0;
      }
      if (typeof item.tmClickRate !== 'undefined') {
        item.tmClickRate = parseFloat(parseFloat(String(item.tmClickRate * 100)).toFixed(2));
      }
      if (typeof item.avgWordSeRiseRate!=='undefined'){
        item.avgWordSeRiseRate = parseFloat(parseFloat(String(item.avgWordSeRiseRate * 100)).toFixed(2));
      }
    });
    data.value = tableData.data;
    loading.value = false;
    // console.log(tableData);

    visible.value = true;
  }
}
</script>

<template>
  <base-app>
    <a-button-group>
      <user style="margin-right: 0"/>
      <a-button :loading="loading" size="small" type="primary" @click="handle">一键转化</a-button>
    </a-button-group>
    <user-login-modal v-model:login-visible="loginVisible"/>
    <search-rank-modal v-model:visible="visible" :data="data" :tab1="tab1" :tab2="tab2"/>
  </base-app>
</template>

<style scoped>

</style>
